﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure.Crosscutting.Authorize
{
    public class MvcActionService : IMvcActionService
    {
        Domain.Uow.IRepository<Domain.Entity.MvcAction>  actionRepository;

        public MvcActionService(Domain.Uow.IRepository<Domain.Entity.MvcAction> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("actionRepository is null");
            }
            actionRepository = action;
        }

        public Domain.Entity.MvcAction Add(Domain.Entity.MvcAction action, Domain.Entity.User operUser)
        {
            try
            {
                actionRepository.Add(action);
                actionRepository.Uow.Commit();
            }
            catch (Exception ex)
            {
            }
            return action;
        }

        public bool Delete(Domain.Entity.MvcAction action, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                action.State = false;
                actionRepository.Modify(action);
                actionRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;

        }

        public bool DeleteTrue(Domain.Entity.MvcAction action, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                actionRepository.Delete(action);
                actionRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Delete(IList<int> idList, Domain.Entity.User operUser)
        {
            var res = false;
            var delete = actionRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                item.State = false;
                actionRepository.Modify(item);
            }
            try
            {
                actionRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(IList<int> idList, Domain.Entity.User operUser)
        {
            var res = false;
            var delete = actionRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                actionRepository.Delete(item);
            }
            try
            {
                actionRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Modify(Domain.Entity.MvcAction action, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                actionRepository.Modify(action);
                actionRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        /// <summary>
        /// Gets the action list.
        /// permission service used
        /// </summary>
        /// <param name="actionIdList">The action id list.</param>
        /// <returns></returns>
        public IQueryable<Domain.Entity.MvcAction> GetList(IQueryable<int> actionIdList)
        {
            var res = actionRepository.GetList(d => actionIdList.Contains(d.Id)).AsQueryable();
            return res;
        }

        public IQueryable<Domain.Entity.MvcAction> GetList()
        {
            var res = actionRepository.GetList().AsQueryable();
            return res;
        }

        public Domain.Entity.MvcAction Get(int id)
        {
            var data = actionRepository.Get(id);
            return data;
        }
    }
}
